package com.example.administrator.about_huanxin.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.about_huanxin.FragmentFactory;
import com.example.administrator.about_huanxin.R;
import com.example.administrator.about_huanxin.fragment.FujinFragment;
import com.example.administrator.about_huanxin.fragment.GerenFragment;
import com.example.administrator.about_huanxin.fragment.TongxunFragment;
import com.example.administrator.about_huanxin.fragment.XiaoxiFragment;
import com.example.administrator.about_huanxin.fragment.ZhiBoFragment;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {


    Class[] fragments = new Class[]{
            FujinFragment.class,
            ZhiBoFragment.class,
            XiaoxiFragment.class,
            TongxunFragment.class,
            GerenFragment.class
    };
    TextView fujin;
    TextView zhibo;
    TextView xiaoxi;
    TextView tongxun;
    TextView geren;

    private FragmentManager fm;
    private FragmentTransaction ft;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        initEvent();
        setTab(0);
    }

    private void initEvent() {
        fujin.setOnClickListener(this);
        zhibo.setOnClickListener(this);
        xiaoxi.setOnClickListener(this);
        tongxun.setOnClickListener(this);
        geren.setOnClickListener(this);
    }

    private void initView() {
        fujin = (TextView) findViewById(R.id.fujin);
        zhibo = (TextView) findViewById(R.id.zhibo);
        xiaoxi = (TextView) findViewById(R.id.xiaoxi);
        tongxun = (TextView) findViewById(R.id.tongxun);
        geren = (TextView) findViewById(R.id.geren);
    }

    private void setTab(int index) {
        fm = getSupportFragmentManager();
        ft = fm.beginTransaction();
        Fragment fragment = fm.findFragmentByTag(index+"");
        hideFragment();
        if(fragment == null){
            ft.add(R.id.container_fram, FragmentFactory.getInstance(fragments[index]),index+"");
        }else{
            ft.show(fragment);
        }

        ft.commit();
    }

    private void hideFragment() {
        for(int i=0;i<5;i++){
            if(fm.findFragmentByTag(i+"") != null){
                ft.hide(fm.findFragmentByTag(i+""));
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.fujin:
                setTab(0);
                break;

            case R.id.zhibo:
                setTab(1);
                break;

            case R.id.xiaoxi:
                setTab(2);
                break;

            case R.id.tongxun:
                setTab(3);
                break;

            case R.id.geren:
                setTab(4);
                break;
        }
    }
}
